package com.test.service;

import com.test.dao.MerchantDao;
import com.test.dao.OpenOrderDao;
import com.test.dao.OpenOrderHaveDao;
import com.test.domain.OpenOrder;
import com.test.domain.OpenOrderFull;
import com.test.domain.OpenOrderHave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class OpenOrderService {

    private final OpenOrderDao openOrderDao;

    private final OpenOrderHaveDao openOrderHaveDao;

    private final MerchantDao merchantDao;

    @Autowired
    public OpenOrderService(OpenOrderDao openOrderDao, OpenOrderHaveDao openOrderHaveDao, MerchantDao merchantDao) {
        this.openOrderDao = openOrderDao;
        this.openOrderHaveDao = openOrderHaveDao;
        this.merchantDao = merchantDao;
    }


    public List<OpenOrderFull> getOpenOrderListC(String customerPhoneNumber){
        List<OpenOrderFull> openOrderList = new ArrayList<>();

        List<String> openOrderNumList = openOrderDao.getOpenOrderNumListByPhoneNumberC(customerPhoneNumber);
        for (String openOrderNum : openOrderNumList) {

            List<OpenOrderHave> dishList = openOrderHaveDao.getDishListByOpenOrderNumber(openOrderNum);
            OpenOrder openOrder = openOrderDao.getOpenOrderByOpenOrderNumber(openOrderNum);
            String shopName= merchantDao.getMerchantByPhoneNumber(openOrder.getMerchantPhoneNumber()).getShopName();
            openOrderList.add(new OpenOrderFull(shopName,openOrder,dishList));

        }

        return openOrderList;
    }

    public List<OpenOrderFull> getOpenOrderListM(String merchantPhoneNumber){
        List<OpenOrderFull> openOrderList = new ArrayList<>();
        List<String> openOrderNumList = openOrderDao.getOpenOrderNumListByPhoneNumberM(merchantPhoneNumber);
        System.out.println(openOrderList);
        for (String openOrderNum : openOrderNumList) {

            List<OpenOrderHave> dishList = openOrderHaveDao.getDishListByOpenOrderNumber(openOrderNum);
            OpenOrder openOrder = openOrderDao.getOpenOrderByOpenOrderNumber(openOrderNum);
            String shopName= merchantDao.getMerchantByPhoneNumber(openOrder.getMerchantPhoneNumber()).getShopName();
            openOrderList.add(new OpenOrderFull(shopName,openOrder,dishList));

        }

        return openOrderList;

    }

    public void updateSituation(String merchantPhoneNumber,String orderNumber){
        openOrderDao.updateSituationByMerchantPhoneNumberAndOrderNumber(merchantPhoneNumber,orderNumber);
    }
}
